package com.sanpu.kxe.rxservice.client;

import android.support.v4.util.Pair;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanpu.kxe.business.trade.bean.BillTypeOutput;
import com.sanpu.kxe.business.trade.bean.FastPayBannerOutput;
import com.sanpu.kxe.business.trade.bean.MyDividendOutput;
import com.sanpu.kxe.business.trade.bean.MyRebTotalOutput;
import com.sanpu.kxe.business.trade.bean.PayCollectionOutput;
import com.sanpu.kxe.business.trade.bean.PayOrderOutput;
import com.sanpu.kxe.business.trade.bean.PayStatusOutput;
import com.sanpu.kxe.business.trade.bean.PaymentOutput;
import com.sanpu.kxe.business.trade.bean.RebTotalOutput;
import com.sanpu.kxe.business.trade.bean.RechargeDetailOutput;
import com.sanpu.kxe.business.trade.bean.SupportBankOutput;
import com.sanpu.kxe.business.trade.bean.TransactionOutput;
import com.sanpu.kxe.business.trade.bean.WithdrawDictinaryOutput;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.rxservice.BaseClient;
import com.sanpu.kxe.rxservice.MetaBean;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.rxservice.interfaces.TradeService;

import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;

/**
 * Description：交易client
 * Created by 薛勇军 on 2018/8/8.
 */

public class TradeClient extends BaseClient {

    private static TradeClient mInstance;

    public static TradeClient getInstance() {
        if (mInstance == null) {
            synchronized (TradeClient.class) {
                if (mInstance == null) {
                    mInstance = new TradeClient();
                }
            }     }
        return mInstance;
    }

    private TradeService service;

    private TradeClient() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.getBaseUrl())
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(TradeService.class);
    }

    /**
     * 我的返佣列表
     */
    public Observable<List<MyRebTotalOutput>> myHstRebateList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.myHstRebateList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MyRebTotalOutput>>>() {
                    @Override
                    public Observable<List<MyRebTotalOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MyRebTotalOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<MyRebTotalOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 我的返佣列表
     */
    public Observable<List<RebTotalOutput>> getMyHstRebateList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.myHstRebateList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<RebTotalOutput>>>() {
                    @Override
                    public Observable<List<RebTotalOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<RebTotalOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<RebTotalOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 获取返佣类型分类
     */
    public Observable<List<BillTypeOutput>> getRebateType() {
        return callMapAfterLogin(service.getRebateType(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<BillTypeOutput>>>() {
                    @Override
                    public Observable<List<BillTypeOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<BillTypeOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<BillTypeOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 提现
     */
    public Observable<String> userWithdraw(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.userWithdraw(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 获取当前的提现限额
     */
    public Observable<WithdrawDictinaryOutput> withdrawDictinary() {
        return callMapAfterLogin(service.withdrawDictinary(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<WithdrawDictinaryOutput>>() {
                    @Override
                    public Observable<WithdrawDictinaryOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        WithdrawDictinaryOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second,
                                    new TypeToken<WithdrawDictinaryOutput>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 账单列表
     */
    public Observable<List<TransactionOutput>> getBillList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.getBillList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<TransactionOutput>>>() {
                    @Override
                    public Observable<List<TransactionOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<TransactionOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<TransactionOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 提现账单列表
     */
    public Observable<List<TransactionOutput>> withDrawBillList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.withDrawBillList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<TransactionOutput>>>() {
                    @Override
                    public Observable<List<TransactionOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<TransactionOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<TransactionOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 获取账单分类
     */
    public Observable<List<BillTypeOutput>> getBillType() {
        return callMapAfterLogin(service.getBillType(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<BillTypeOutput>>>() {
                    @Override
                    public Observable<List<BillTypeOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<BillTypeOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<BillTypeOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 收款渠道列表
     */
    public Observable<List<PayCollectionOutput>> appCnlConfList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.appCnlConfList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<PayCollectionOutput>>>() {
                    @Override
                    public Observable<List<PayCollectionOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<PayCollectionOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<PayCollectionOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 还款渠道列表
     */
    public Observable<List<PayCollectionOutput>> appCnlConfList(JSONObject reqData, String version) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        requestMap.put(CommonConfig.VERSION, version);
        return callMapAfterLogin(service.appCnlConfList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<PayCollectionOutput>>>() {
                    @Override
                    public Observable<List<PayCollectionOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<PayCollectionOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<PayCollectionOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 获取APP通道配置银行限额列表
     */
    public Observable<List<SupportBankOutput>> findAppCnlConfBankSupportDetailList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.findAppCnlConfBankSupportDetailList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<SupportBankOutput>>>() {
                    @Override
                    public Observable<List<SupportBankOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<SupportBankOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<SupportBankOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 添加信用卡支持银行
     */
    public Observable<List<SupportBankOutput>> appBaseBank() {
        return callMapAfterLogin(service.appBaseBank(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<SupportBankOutput>>>() {
                    @Override
                    public Observable<List<SupportBankOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<SupportBankOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second,
                                    new TypeToken<List<SupportBankOutput>>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 生成支付订单
     */
    public Observable<PaymentOutput> userPayment(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.userPayment(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<PaymentOutput>>() {
                    @Override
                    public Observable<PaymentOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        PaymentOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<PaymentOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 快捷native收款广告
     */
    public Observable<List<FastPayBannerOutput>> lvlFee() {
        return callMapAfterLogin(service.lvlFee(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<FastPayBannerOutput>>>() {
                    @Override
                    public Observable<List<FastPayBannerOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<FastPayBannerOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<FastPayBannerOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 获取归属地和话费流量充值套餐信息（首页=>话费流量）
     */
    public Observable<RechargeDetailOutput> getRechargeDetail(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.getRechargeDetail(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<RechargeDetailOutput>>() {
                    @Override
                    public Observable<RechargeDetailOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        RechargeDetailOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second,
                                    new TypeToken<RechargeDetailOutput>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 话费充值（首页=>话费流量=>立即充值）
     */
    public Observable<String> recharge(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.recharge(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 统一下单
     */
    public Observable<PayOrderOutput> payOrder(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.payOrder(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<PayOrderOutput>>() {
                    @Override
                    public Observable<PayOrderOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        PayOrderOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second,
                                    new TypeToken<PayOrderOutput>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 获取支付状态
     */
    public Observable<PayStatusOutput> findTranPayStatus(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.findTranPayStatus(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<PayStatusOutput>>() {
                    @Override
                    public Observable<PayStatusOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        PayStatusOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second,
                                    new TypeToken<PayStatusOutput>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 我的分红
     */
    public Observable<MyDividendOutput> myDividend() {
        return callMapAfterLogin(service.myDividend(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<MyDividendOutput>>() {
                    @Override
                    public Observable<MyDividendOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        MyDividendOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second,
                                    new TypeToken<MyDividendOutput>() {}.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }
}
